﻿unit Srv_Tk_RDataUnit;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes,Forms, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, KS_Tk_Srv_TLB, DB, ADODB, Provider,PwdFunUnit, MidasLib,
  Dialogs, StdVcl;

type
  TKSTk_Srv = Class(TRemoteDataModule, IKSTk_Srv)
    ADOConnection1: TADOConnection;
    tmp_DataSetProvider: TDataSetProvider;
    tmp_DataSet: TADODataSet;
    Update_DataSetProvider: TDataSetProvider;
    Update_ADODataSet: TADODataSet;
    Da_DataSetProvider: TDataSetProvider;
    Da_ADODataSet: TADODataSet;
    St_DataSetProvider: TDataSetProvider;
    St_ADODataSet: TADODataSet;
    Srv_DataSetProvider: TDataSetProvider;
    Srv_DataSet: TADODataSet;
    Bj_DataSetProvider: TDataSetProvider;
    Bj_ADODataSet: TADODataSet;
    Tkb_DataSetProvider: TDataSetProvider;
    Tkb_ADODataSet: TADODataSet;
    Bxdab_ADODataSet: TADODataSet;
    Tkfjb_ADODataSet: TADODataSet;
    Gjcxb_ADODataSet: TADODataSet;
    Bxdab_DataSetProvider: TDataSetProvider;
    Tkfjb_DataSetProvider: TDataSetProvider;
    Gjcxb_DataSetProvider: TDataSetProvider;
    Tkb_DS: TDataSource;
    Tkfjb_DS: TDataSource;
    procedure RemoteDataModuleCreate(Sender: TObject);
    procedure ADOConnection1AfterConnect(Sender: TObject);
    procedure ADOConnection1AfterDisconnect(Sender: TObject);
    procedure Tkb_ADODataSetAfterOpen(DataSet: TDataSet);
    procedure tmp_DataSetProviderGetData(Sender: TObject;
      DataSet: TCustomClientDataSet);
    procedure tmp_DataSetProviderUpdateData(Sender: TObject;
      DataSet: TCustomClientDataSet);
    procedure Bxdab_ADODataSetBeforePost(DataSet: TDataSet);
    procedure Tkfjb_ADODataSetBeforePost(DataSet: TDataSet);
    procedure Gjcxb_ADODataSetBeforePost(DataSet: TDataSet);
    procedure Tkfjb_ADODataSetAfterOpen(DataSet: TDataSet);
    procedure tmp_DataSetProviderUpdateError(Sender: TObject;
      DataSet: TCustomClientDataSet; E: EUpdateError; UpdateKind: TUpdateKind;
      var Response: TResolverResponse);
  private
    //v_host,v_db,v_sa,v_pwd,connect_str :string;
    { Private declarations }
    All_Jj_Kcdm : string;
    function Get_Tk_MaxID():Integer;
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
    function Create_Random_SJ(const KaoHao, Kcdm, Host_Name,
      Host_IP: WideString): OleVariant; safecall;
    function Get_Is_Readied: WordBool; safecall;
    procedure Get_Srv_Status(const Kcdm: WideString; out is_ok,
      re_result: OleVariant); safecall;
    procedure Get_Ks_Status(const KaoHao, Kcdm: WideString;
      var Ks_Status: OleVariant); safecall;
    function Del_Ks_Sj(const KaoHao, Kcdm: WideString): OleVariant; safecall;
    procedure Clear_Ks_SjDa(const KaoHao, Kcdm: WideString;
      var Is_Sucessed: OleVariant); safecall;
    procedure Get_Ks_SyTime(const KaoHao, Kcdm: WideString;
      var re_SyTime: OleVariant); safecall;
    procedure Set_Ks_Jj(const KaoHao, Kcdm: WideString;
      var Is_Sucessed: OleVariant); safecall;
    procedure Set_Ks_Status(const KaoHao, Kcdm, Ks_Status: WideString;
      var Is_Sucessed: OleVariant); safecall;
    procedure Set_Ks_Wb(const KaoHao, Kcdm: WideString;
      var Is_Sucessed: OleVariant); safecall;
    function Get_AddTimePwd(const Kcdm: WideString): OleVariant; safecall;
    function Get_CKPwd(const Kcdm: WideString): OleVariant; safecall;
    function Get_Is_Xcgf(const Kcdm: WideString): OleVariant; safecall;
    procedure Set_Ks_StartKs(const KaoHao, Kcdm, Zkzh, Host_Name,
      Host_IP: WideString; var Is_Sucessed: OleVariant); safecall;
    procedure Set_Ks_Ck(const KaoHao, Kcdm: WideString;
      var Is_Sucessed: OleVariant); safecall;
    procedure Set_Ks_Fj(const KaoHao, Kcdm: WideString;
      var Is_Sucessed: OleVariant); safecall;
    function Get_Ks_is_Exists(const KaoHao: WideString): OleVariant; safecall;
    function Get_Ks_in_Ksb(const KaoHao, Kcdm: WideString): OleVariant;
      safecall;
    function Get_Ks_is_OK(const KaoHao, Kcdm, KsName: WideString;
      var Ks_Status: OleVariant): OleVariant; safecall;
    function Get_Zkzh_is_OK(const Zkzh, ZkzPwd, KaoHao: WideString;
      var zkz_Status: OleVariant): OleVariant; safecall;
    function Get_Ks_Name(const KaoHao: WideString): OleVariant; safecall;
    function Get_Ks_Cj(const KaoHao, Kcdm: WideString): OleVariant; safecall;
    function Get_Is_Mnks(const Kcdm: WideString): OleVariant; safecall;
    function Get_Is_Started(const Kcdm: WideString): OleVariant; safecall;
    procedure Auto_Jj; safecall;
    function Get_Is_KsTime(const Kcdm: WideString): OleVariant; safecall;
    function Get_Is_Check_Zkz(const Kcdm: WideString): OleVariant; safecall;
    function Get_Table_ID(const vTableName: WideString): OleVariant; safecall;
    function Init_TmpSjpzb(const Kcdm: WideString): OleVariant; safecall;
    function Is_HasBzda(const Kcdm, SjTx: WideString): OleVariant; safecall;
    function Get_TxLb(const Kcdm, SjTx: WideString): OleVariant; safecall;
    function Get_XhLength: OleVariant; safecall;
    function Dafj_Exists(const Kcdm, KaoHao, Dafjm: WideString): OleVariant;
      safecall;
    function Check_Teacher(const Teacher_Num,
      Teacher_Pwd: WideString): OleVariant; safecall;
    function Kc_Is_Submited(const Teacher_Num, Kcdm: WideString): OleVariant;
      safecall;
    function Get_Is_Check_Ks(const Kcdm: WideString): OleVariant; safecall;
    function Get_Tx_Fs(const Kcdm, Tx: WideString): OleVariant; safecall;
    function Get_Tx(const Kcdm, Txlb: WideString): OleVariant; safecall;
    function Get_Sub_St_Count(const St_ID: WideString): Integer; safecall;
    procedure Judge_SingleSelect(const Kcdm, Tx: WideString); safecall;
    procedure Judge_Pdt(const Kcdm, Tx: WideString); safecall;
    procedure Judge_MutilSelect(const Kcdm, Tx: WideString); safecall;
    procedure Judge_Pbt(const Kcdm, Tx, Sub_Count: WideString); safecall;
    procedure Judge_Tkt(const Kcdm, Tx, Qxdxx: WideString); safecall;
    procedure Judge_Dwt(const Kcdm, Tx, Sub_Count: WideString); safecall;
    procedure Sum_Fs(const Kcdm: WideString); safecall;
    function AddTimeLength(const Kcdm: WideString): OleVariant; safecall;
    function AddTimePwd(const Kcdm: WideString): OleVariant; safecall;
    function Get_Kgt_Cj(const Kcdm, KaoHao: WideString): OleVariant; safecall;
    function Get_Is_All_Jj(const Kcdm: WideString): OleVariant; safecall;
    procedure Set_All_Jj_Kcdm(const Kcdm, Is_All_Jj: WideString); safecall;
    function Get_CON_Db_IP: OleVariant; safecall;
    function Get_CON_Db_Name: OleVariant; safecall;
    function Get_CON_Sa_Name: OleVariant; safecall;
    function Get_CON_Sa_Pwd: OleVariant; safecall;
    function Get_Cur_Kddm: OleVariant; safecall;
    function Get_Tkb_MaxID: Integer; safecall;
    function Get_Tkfjb_MaxID: Integer; safecall;
    procedure Refresh_InKeyTable; safecall;
    function Set_Ks_Code(const Kcdm: WideString): OleVariant; safecall;
    function Get_Ks_Time_Length(const Kcdm: WideString): OleVariant; safecall;
    function Set_Dzcs_Count(const Kcdm: WideString; iCount: Integer): OleVariant;
      safecall;
    function Get_Dzcs_Count(const Kcdm: WideString): Integer; safecall;
  public
    { Public declarations }
  end;


implementation
uses Srv_SrvSetUnit,AboutUnit;
{$R *.DFM}

class procedure TKSTK_Srv.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

function TKSTk_Srv.Create_Random_SJ(const KaoHao, Kcdm, Host_Name,
  Host_IP: WideString): OleVariant;
var
  sj_set_query:tadoquery; //试卷配置表
  ks_ksxx_query:tadoquery; //考生考试信息表
  ks_sj_query:tadoquery; //考生试卷表
  tmp_query,sub_query,tk_query :tadoquery;
  i,ii:integer;
  txzsd_count_str,tx_str,zsd_str,nyd_str,da_str:string;
  //tmpStrings :TStrings;
begin
  try
    //tmpstrings:= TStringList.Create;
    adoconnection1.BeginTrans;

    sj_set_query := tadoquery.Create(nil);
    sj_set_query.Connection := AdoConnection1;

    tmp_query := tadoquery.Create(nil);
    tmp_query.Connection := adoconnection1;

    sub_query := tadoquery.Create(nil); //大题中的小题
    sub_query.Connection := adoconnection1;

    tk_query := tadoquery.Create(nil);
    tk_query.Connection := adoconnection1;

    try
      Del_Ks_Sj(KaoHao,Kcdm);
      with tmp_query do
      begin
        //-----------写入考生考试信息----------------//
        Close;
        SQL.Clear;
        SQL.Add('insert into 考生考试表 (课程号,学号,考试时长,考试状态,考生机器名,考生IP)');
        sql.Add('select top 1 课程号,'+quotedstr(KaoHao)+',考试时长,'+quotedstr('尚未开考'));
        sql.add(','+quotedstr(Host_Name)+','+quotedstr(Host_ip)+' from 考试配置表' );
        sql.Add('where 课程号='+quotedstr(Kcdm));
        //sql.SaveToFile('抽题.txt');
        ExecSQL;
        close;
        //----------------   end  ------------------//


        //------------打开试卷配置表-----------//
        sj_set_query.close;
        sj_set_query.sql.Clear;
        sj_set_query.sql.Add('select * from 试卷配置表 where 课程号='+quotedstr(Kcdm)+' order by 题号');
        sj_set_query.open;
        //---------------end-------------------//

        sj_set_query.first;
        i:=1;

        //-------------为考生抽题--------------//
        while not sj_set_query.eof do
        begin
          txzsd_count_str := sj_set_query.fieldbyname('题量').AsString;
          tx_str := quotedstr(sj_set_query.fieldbyname('题型').AsString);
          zsd_str := quotedstr(sj_set_query.fieldbyname('知识点').AsString);
          nyd_str := quotedstr(sj_set_query.fieldbyname('难易度').AsString);

          //--------------------  更好的方法 ------------------//
          {
          Close;
          SQL.Clear;
          SQL.Add('insert into 考生试卷表 (学号,题号,题型,知识点,难易度,题目ID)');
          sql.Add('select top '+txzsd_count_str+' '+quotedstr(KaoHao)+','+inttostr(i)+','+tx_str+','+zsd_str+','+nyd_str+',ID from 题库表');
          sql.Add('where 题型='+tx_str+' and 知识点='+zsd_str+' and 难易度='+nyd_str+' order by newid()');
          prepared := true;
          sql.SaveToFile('kk.txt');
          ExecSQL;
          close;
          }
          //--------------------  END更好的方法 ------------------//

          //--------------------没办法的办法 ---------------------//
          //就因为一个题号不能自动生成的问题，只好用循环来处理

          tk_query.Close;
          tk_query.sql.clear;
          tk_query.sql.Add('select top '+txzsd_count_str+' tk.ID from 题库表 tk');
          //tk_query.SQL.add('inner join 试卷配置表 sjpz on sjpz.题型=tk.题型 and sjpz.知识点=tk.知识点');
          tk_query.SQL.add('where pid is null and tk.题型='+tx_str+' and tk.知识点='+zsd_str+' and 难易度='+nyd_str);
          tk_query.SQL.Add('and tk.课程号='+quotedstr(Kcdm)+' order by newid()');
          //Tk_query.sql.savetofile('ksks.txt');
          tk_query.Open;

          tk_query.First;
          while not tk_query.Eof do
          begin
            //if tk_query.fieldbyname('答案').IsNull then
            //  da_str := 'null'
            //else
            //  da_str := quotedstr(tk_query.fieldbyname('答案').Value);

            Close;
            SQL.Clear;
            SQL.Add('insert into 考生试卷表 (课程号,学号,题号,小题号,题型,知识点,难易度,题目ID)');
            sql.Add('values('+quotedstr(Kcdm)+','+quotedstr(KaoHao)+','+inttostr(i)+',0,'+tx_str+','+zsd_str+','+nyd_str+','+tk_query.fieldbyname('ID').AsString+')');
            prepared := true;
            //sql.SaveToFile('inserttosj.txt');
            //tmpstrings.Add(sql.Text);
            ExecSQL;
            close;

            //=================插入子题目==================
            sub_query.Close;
            sub_query.sql.clear;
            sub_query.sql.Add('select * from 题库表 tk ');
            sub_query.SQL.Add('where tk.pid='+tk_query.fieldbyname('id').AsString+' order by id');
            sub_query.Open;

            ii := 1;
            while not sub_query.eof do
            begin
              //i := i+1;
              Close;
              SQL.Clear;
              SQL.Add('insert into 考生试卷表 (课程号,学号,题号,小题号,题型,知识点,难易度,题目ID)');
              sql.Add('values('+quotedstr(Kcdm)+','+quotedstr(KaoHao)+','+inttostr(i)+','+inttostr(ii)+','+tx_str+','+zsd_str+','+nyd_str+','+sub_query.fieldbyname('ID').AsString+')');
              //tmpstrings.Add(sql.Text);
              prepared := true;
              ExecSQL;
              Close;
              ii := ii+1;
              sub_query.Next;
            end;
            sub_query.Close;
            //=================end 插入子题目==============

            tk_query.Next;
            i:=i+1;
          end;
          tk_query.Close;

          //----------------结束没有办法的办法--------------------//
          //Parameters.ParamByName('icount').Value := sj_set_query.fieldbyname('题量').AsInteger;
          //Parameters.ParamByName('xh').Value := KaoHao;
          //Parameters.ParamByName('thindex').Value := i;
          //Parameters.ParamByName('tx').Value := sj_set_query.fieldbyname('题型').AsString;
          //Parameters.ParamByName('zsd').Value := sj_set_query.fieldbyname('知识点').AsString;


          sj_set_query.Next;

        end;
        sj_set_query.Close;
        //-----------------结束抽题----------------------//
      end;
      //----------end with tmp_query ... ----------------//

      AdoConnection1.CommitTrans;
      Result := True;
    except
      AdoConnection1.rollbackTrans;
      Result := False;
    end;
  finally
    //tmpstrings.SaveToFile('InserttoDb.txt');
    //tmpstrings.Free;
    sj_set_query.Free;
    tmp_query.Free;
    sub_query.Free;
    tk_query.Free;
  end;
  
end;

function TKSTK_Srv.Get_Is_Readied: WordBool;
begin

end;

procedure TKSTK_Srv.Get_Srv_Status(const Kcdm: WideString; out is_ok,
  re_result: OleVariant);
var
  tmp_query:tadoquery;
  tmp_str :string;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.add('SELECT top 1 getdate() AS vtoday, 是否启用,考试开始日期,DATEADD(Hour,24,考试结束日期) as 考试结束日期 FROM 考试配置表');
      sql.Add(' where 课程号='+quotedstr(Kcdm));
      open;

      is_ok := false;

      if (fields[0].AsDateTime >= fields[2].asDateTime) and
         (fields[0].AsDateTime < fields[3].AsDateTime) then
      begin
        re_result := '现在在考试时段之内！';
        is_ok := true;
      end
      else
      begin
        re_result := '现在不在考试时间之内！';
        is_ok := false;
      end;

      if ((is_ok=true) and (fields[1].value = '是')) or
         ((is_ok<>true) and (fields[1].value <> '是')) then
         tmp_str := '且'
      else
         tmp_str := '但';

      if fields[1].value = '是' then
         re_result := re_result+tmp_str+'考试服务器已启用！'
      else
      begin
         re_result := re_result+tmp_str+'考试服务器已关闭！';
         is_ok := false;
      end;

      close;
    end;
  finally
    tmp_query.Free;
  end;

end;

procedure TKSTK_Srv.Get_Ks_Status(const KaoHao, Kcdm: WideString;
  var Ks_Status: OleVariant);
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 考试状态 from 考生考试表 where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));

      Open;
      if RecordCount = 0 then
        Ks_Status := ''
      else
        Ks_Status := fields[0].AsString;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

//=================删除考生试卷====================//
function TKSTk_Srv.Del_Ks_Sj(const KaoHao, Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('delete from 考生考试表 where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));
      try
        execsql;
        Result := True;
      except
        Result := false;
      end;
    end;
  finally
    tmp_query.Free;
  end;
end;

//===================清除考生试卷答案===================//
procedure TKSTK_Srv.Clear_Ks_SjDa(const KaoHao, Kcdm: WideString;
  var Is_Sucessed: OleVariant);
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('delete 答案附件表 where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));
      try
        execsql;
        is_sucessed := True;
      except
        is_sucessed := false;
      end;

      sql.clear;
      sql.Add('update 考生试卷表 set 答案=Null,答案附件=NULL,答案附件文件名=NULL,得分=NULL where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));
      try
        execsql;
        is_sucessed := True;
      except
        is_sucessed := false;
      end;
    end;
  finally
    tmp_query.Free;
  end;
end;

//=======================得到考生剩余考试时间======================//
procedure TKSTK_Srv.Get_Ks_SyTime(const KaoHao, Kcdm: WideString;
  var re_SyTime: OleVariant);
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 开考时间,交卷时间,DATEDIFF(minute, 开考时间, GETDATE()) AS 已用时间,考试时长,考试状态 from 考生考试表 where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));
      //sql.SaveToFile('剩余时间.txt');
      Open;
      
      if RecordCount = 0 then
        re_SyTime := 0
      else if (fieldbyname('考试状态').AsString = '考完交卷') or
              (fieldbyname('考试状态').AsString = '舞弊') then
        re_SyTime := 0
      else if (fieldbyname('考试状态').AsString = '尚未开考') or
              fieldbyname('开考时间').IsNull then
        re_SyTime := fieldbyname('考试时长').AsInteger
      else if (fieldbyname('考试状态').AsString = '正在考试') or
              (fieldbyname('考试状态').AsString = '换机继考') then
        re_SyTime := fieldbyname('考试时长').AsInteger-fieldbyname('已用时间').AsInteger;
        
    end;
  finally
    tmp_query.Free;
  end;
end;

//-------------------------设置考生考试状态------------------------//
procedure TKSTK_Srv.Set_Ks_Status(const KaoHao, Kcdm,
  Ks_Status: WideString; var Is_Sucessed: OleVariant);
var
  sqlStr:string;
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      if Ks_Status='抽题重考' then
        sqlStr := '开考时间=GetDate(),'
      else
        sqlStr := '交卷时间=GetDate(),';
        
      sqlStr := 'update 考生考试表 set '+sqlStr+'考试状态='+quotedstr(Ks_Status)+
                ' where 学号='+quotedstr(KaoHao)+' and 课程号='+quotedstr(Kcdm);
      sql.Add(sqlStr);
      try
        execsql;
        is_sucessed := True;
      except
        is_sucessed := false;
      end;
    end;
  finally
    tmp_query.Free;
  end;
end;

//======================考生考完交卷==========================//
procedure TKSTK_Srv.Set_Ks_Jj(const KaoHao, Kcdm: WideString;
  var Is_Sucessed: OleVariant);
begin
  Set_Ks_Status(KaoHao,kcdm,'考完交卷',is_Sucessed);
end;

//============================设置考生为舞弊状态================//
procedure TKSTK_Srv.Set_Ks_Wb(const KaoHao, Kcdm: WideString;
  var Is_Sucessed: OleVariant);
begin
  Set_Ks_Status(KaoHao,Kcdm,'舞弊',is_Sucessed);
end;

function TKSTK_Srv.Get_AddTimePwd(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 补时密码 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_CKPwd(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 重考密码 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Is_Xcgf(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 现场给分 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value='是';
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

//--------------------------设置考生为开考状态----------------//
procedure TKSTK_Srv.Set_Ks_StartKs(const KaoHao, Kcdm, Zkzh, Host_Name,
  Host_IP: WideString; var Is_Sucessed: OleVariant);
var
  tmp_query:tadoquery;
  ks_status:Olevariant;
begin
  try
    try
      Adoconnection1.BeginTrans;
      is_Sucessed := False;
      Get_Ks_Status(KaoHao,Kcdm,ks_status);
      tmp_query := tadoquery.Create(nil);
      with tmp_query do
      begin
        close;
        connection := adoconnection1;
        sql.clear;
        sql.Add('update 考生考试表 set ');
        if ks_status = '尚未开考' then    //如果是换机继考的话，不能重设开考时间
          sql.add('开考时间=GetDate(),');

        sql.Add('考生机器名='+quotedstr(Host_Name)+',考生IP='+quotedstr(Host_IP)+',考试状态='+quotedstr('正在考试')+' where 学号='+quotedstr(KaoHao));
        sql.add('and 课程号='+quotedstr(Kcdm));
        execsql;
        close;

        //====================为准考证表加上学号的标识===============//
        close;
        sql.Clear;
        sql.add('update 准考证表 set 学号='+quotedstr(KaoHao));
        sql.Add('where 准考证号='+quotedstr(Zkzh));
        execsql;
        Close;
      end;
      AdoConnection1.CommitTrans;
      Is_sucessed := True;
    except
      Is_sucessed := False;
      AdoConnection1.RollbackTrans;
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.RemoteDataModuleCreate(Sender: TObject);
begin
  All_Jj_Kcdm := '';

  with ADOConnection1 do
  begin
    Close;
    ConnectionString := '';
    if gbDBConnStr='' then gbDBConnStr := Get_Connect_Str;
    ConnectionString := gbDBConnStr;
      //----------------------------------------------------//
  end;
end;

procedure TKSTK_Srv.ADOConnection1AfterConnect(Sender: TObject);
begin
  //Refresh_MaxID(ADOConnection1);
  //Srv_Mainform.StatusBarEx1.Panels.Items[0].Text := '数据库服务器：'
  //Srv_MainForm.StatusBarEx1.Panels.Items[1].Text := v_host;
end;

procedure TKSTK_Srv.ADOConnection1AfterDisconnect(Sender: TObject);
begin
  //Srv_MainForm.StatusBarEx1.Panels.Items[1].Text := '连接已断开……';
end;

procedure TKSTK_Srv.Set_Ks_Ck(const KaoHao, Kcdm: WideString;
  var Is_Sucessed: OleVariant);
begin
  Set_Ks_Status(KaoHao,Kcdm,'抽题重考',is_Sucessed);
end;

procedure TKSTK_Srv.Set_Ks_Fj(const KaoHao, Kcdm: WideString;
  var Is_Sucessed: OleVariant);
begin
  Set_Ks_Status(KaoHao,Kcdm,'换机继考',is_Sucessed);
end;

function TKSTK_Srv.Get_Ks_is_Exists(const KaoHao: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select count(*) from 考生信息表 where 学号='+quotedstr(KaoHao));
      open;
      Result := fields[0].Value>0;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Ks_in_Ksb(const KaoHao,
  Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select count(*) from 考生考试表 where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value>0;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Ks_is_OK(const KaoHao, Kcdm, KsName: WideString;
  var Ks_Status: OleVariant): OleVariant;
var
  vkd:string;
  Str:Olevariant;
  tmp_query:tadoquery;
begin
  Result := False;
  if not Get_Is_Check_Ks(Kcdm) then //要求考生信息必须存在
  begin
    //====================================================//
    if Get_Ks_is_Exists(KaoHao) = False then
    begin
      try
        tmp_query := tadoquery.Create(nil);
        with tmp_query do
        begin
          close;
          connection := adoconnection1;
          sql.clear;
          sql.Add('select * from 考点信息表');
          open;
          if Recordcount<>1 then
            vkd := ''
          else
            vkd := fieldbyname('考点代码').asstring;

          close;
          sql.clear;
          if vkd = '' then
          begin
            sql.Add('insert into 考生信息表 (学号,姓名) values (:xh,:xm)');
          end
          else
          begin
            sql.Add('insert into 考生信息表 (学号,姓名,所属考点) values (:xh,:xm,:kd)');
            Parameters.ParamByName('kd').Value := vkd;
          end;

          Parameters.ParamByName('xh').Value := KaoHao;
          Parameters.ParamByName('xm').Value := KsName;

          try
            ExecSql;
            ks_Status := '验证成功';
            Result := True;
          except
            Ks_Status := '插入学生信息时出错';
          end;
        end;
      finally
        tmp_query.Free;
      end;
    end;  //end if
    //====================================================//
  end;


  if Get_Ks_is_Exists(KaoHao) = False then
    ks_Status := '该学号不存在'
  else
    if Get_Ks_Name(KaoHao) <> ksName then
       ks_Status := '学号和姓名不一致'
    else
    begin
       Result := True;
       ks_Status := '验证成功';
    end;

  //Get_Ks_Status(KaoHao,Ks_Status);

  //if Ks_Status<>'正在考试' then
  //  Result := True;

end;

function TKSTK_Srv.Get_Zkzh_is_OK(const Zkzh, ZkzPwd, KaoHao: WideString;
  var zkz_Status: OleVariant): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    Result := False;
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 准考证号,考试密码,学号 from 准考证表 where 准考证号='+quotedstr(Zkzh));
      open;
      if RecordCount<1 then
         zkz_Status := '该准考证号不存在'
      else
         if fieldbyname('考试密码').AsString <> ZkzPwd then
            zkz_Status := '准考证密码错误'
         else
            if (fieldbyname('学号').AsString <> '') and
               (fieldbyname('学号').AsString <> KaoHao) then
               zkz_Status := '该准考证号不是此考生的'
            else
            begin
               zkz_Status := '验证成功';
               Result := True;
            end;

      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Ks_Name(const KaoHao: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 姓名 from 考生信息表 where 学号='+quotedstr(KaoHao));
      open;
      if Fields[0].IsNull then
         Result := ''
      else
         Result := fields[0].AsString;
         
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Ks_Cj(const KaoHao, Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select Sum(得分) from 考生试卷表 where 学号='+quotedstr(KaoHao));
      sql.add('and 课程号='+quotedstr(Kcdm));
      open;
      if Fields[0].IsNull then
         Result := 0.0
      else
         Result := fields[0].AsFloat;
         
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Is_Mnks(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 开放模拟 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value='是';
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Is_Started(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 是否启用 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value='是';
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Auto_Jj;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('UPDATE 考生考试表 SET 考试状态='+quotedstr('考完交卷')+', 交卷时间=DATEADD(minute, 考试时长, 开考时间)');
      sql.add('WHERE (考试状态<>'+quotedstr('考完交卷')+') AND (DATEDIFF(minute,开考时间, GETDATE())>=考试时长)');
      Prepared := true;
      Execsql;
      
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Is_KsTime(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.add('SELECT top 1 getdate() AS vtoday, 考试开始日期,DATEADD(Hour,24,考试结束日期) as 考试结束日期 FROM 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;

      Result := (fields[0].AsDateTime >= fields[1].asDateTime) and
                (fields[0].AsDateTime < fields[2].AsDateTime) ;
      close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Is_Check_Zkz(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 是否验证准考证 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value='是';
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Table_ID(const vTableName: WideString): OleVariant;
var
  tbname:String;
  tmp_query:tadoquery;
begin
  try
    tbname := vTableName;
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select max(id) from '+tbname);
      open;
      Result := fields[0].AsInteger+1;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Init_TmpSjpzb(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('delete from tmp_试卷配置表');
        execsql;

        close;
        sql.clear;
        sql.Add('insert into tmp_试卷配置表 (课程号,题型,知识点,难易度,现有题量,题量,分数,题号)');
        sql.add('select a.课程号,a.题型,a.知识点,a.难易度,a.现有题量,b.题量,b.分数,b.题号 from VIEW_题型知识点难易度题量表 a');
        sql.add('left outer join 试卷配置表 b on b.课程号=a.课程号 and b.题型=a.题型 and b.知识点=a.知识点 and b.难易度=a.难易度');
        sql.add('where a.课程号='+quotedstr(kcdm));
        Execsql;

        Close;
      end;
      Result := True;
    except
      Result := False;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Is_HasBzda(const Kcdm, SjTx: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := False;
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select 有标准答案 from view_题型信息');
        sql.add('where 课程号='+quotedstr(kcdm));
        sql.add('and 题型='+quotedstr(SjTx));
        Open;
        Result := fields[0].AsString = '是';
      end;
    except
      Result := False;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_TxLb(const Kcdm, SjTx: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := '';
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select 题型类别 from view_题型信息');
        sql.add('where 课程号='+quotedstr(kcdm));
        sql.add('and 题型='+quotedstr(SjTx));
        Open;
        Result := fields[0].AsString ;
      end;
    except
      Result := '';
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_XhLength: OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := -1;
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select distinct 考号位数 from 考试配置表');
        //sql.add('where 课程号='+quotedstr(kcdm));
        //sql.add('and 题型='+quotedstr(SjTx));
        Open;
        Result := fields[0].AsInteger ;
      end;
    except
      Result := -1;
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Tkb_ADODataSetAfterOpen(DataSet: TDataSet);
begin
  if not BxDab_ADODataSet.Active then
     BxDab_ADODataSet.Open;
  if not Tkfjb_AdoDataSet.Active then
     Tkfjb_AdoDataSet.Open;
end;

function TKSTK_Srv.Dafj_Exists(const Kcdm, KaoHao,
  Dafjm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := False;
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select count(id) from 答案附件表');
        sql.add('where 课程号='+quotedstr(kcdm));
        sql.add('and 学号='+quotedstr(KaoHao));
        sql.Add('and 答案附件文件名='+quotedstr(Dafjm));
        Open;
        Result := fields[0].Value>0 ;
      end;
    except
      Result := False;
    end;
  finally
    tmp_query.Free;
  end;
end;
                    
function TKSTK_Srv.Check_Teacher(const Teacher_Num,
  Teacher_Pwd: WideString): OleVariant;
var
  tmp_query:tadoquery;
  Res_Str:String;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Res_Str := '查询语句出错！';
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select 是否启用 from 任课教师表');
        sql.add('where 教师编号='+quotedstr(Teacher_Num));
        sql.add('and 密码='+quotedstr(Teacher_Pwd));
        Open;

        if RecordCount = 0 then
           Res_Str := '教师编号或密码验证错误！'
        else if fieldbyname('是否启用').AsString<>'是' then
           Res_Str := '主观题改卷功能没有开启！'
        else
        begin
          Res_Str := '';
        end;
        Close;
      end;
    except
      Result := False;
    end;
  finally
    Result := Res_Str;
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Kc_Is_Submited(const Teacher_Num,
  Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    Result := False;
    tmp_query := tadoquery.Create(nil);

    with tmp_query do
    begin
      close;
      connection := adoconnection1;

      sql.clear;
      sql.add('select 是否上传 from 教师课程表');
      sql.add('where 教师编号='+quotedstr(Teacher_Num));
      sql.add('and 课程号='+quotedstr(Kcdm));
      Open;

      Result := Trim(fieldbyname('是否上传').AsString)='是';
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;


function TKSTK_Srv.Get_Is_Check_Ks(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 是否验证学号 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value='是';
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Tx_Fs(const Kcdm, Tx: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    Result := 0.0;
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.Clear;
      sql.add('select top 1 分数 from 试卷配置表 where 课程号='+quotedstr(Kcdm)+' and 题型='+quotedstr(Tx));
      Open;
      Result := fields[0].asfloat;
      close;
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Tx(const Kcdm, Txlb: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := '';
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select 题型 from view_题型信息');
        sql.add('where 课程号='+quotedstr(kcdm));
        sql.add('and 题型类别='+quotedstr(Txlb));
        Open;
        Result := fields[0].AsString ;
      end;
    except
      Result := '';
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Sub_St_Count(const St_ID: WideString): Integer;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := 0;
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('select count(pid) from 题库表');
        sql.add('where PID='+St_ID);
        Open;
        Result := fields[0].AsInteger ;
      end;
    except
      Result := 0;
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Judge_SingleSelect(const Kcdm, Tx: WideString);
var
  tmp_query:tadoquery;
  fs:Double;
  //Tx:String;
begin
  try
    //Tx := Get_Tx(Kcdm,'单项选择题');
    fs := Get_Tx_Fs(Kcdm,Tx);

    tmp_query := tadoquery.Create(nil);
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT tk.题型, tk.标准答案, sj.答案, sj.得分 FROM 考生试卷表 sj INNER JOIN ');
        sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx)+' AND tk.课程号='+quotedstr(kcdm));
        Open;
        while not eof do
        begin
          Edit;
          if UpperCase(fieldbyname('标准答案').AsString) = UpperCase(fieldbyname('答案').Asstring) then
             fieldbyname('得分').Value := fs
          else
             fieldbyname('得分').Value := 0;
          Post;
          Next;
        end;
        Close;
      end;
    except
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Judge_Pdt(const Kcdm, Tx: WideString);
var
  tmp_query:tadoquery;
  fs:Double;
  //Tx:String;
begin
  try
    //Tx := Get_Tx(Kcdm,'判断题');
    fs := Get_Tx_Fs(Kcdm,Tx);

    tmp_query := tadoquery.Create(nil);
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT tk.题型, tk.标准答案, sj.答案, sj.得分 FROM 考生试卷表 sj INNER JOIN ');
        sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx)+' AND tk.课程号='+quotedstr(kcdm));
        Open;
        while not eof do
        begin
          Edit;
          if UpperCase(fieldbyname('标准答案').AsString) = UpperCase(fieldbyname('答案').Asstring) then
             fieldbyname('得分').Value := fs
          else
             fieldbyname('得分').Value := 0;
          Post;
          Next;
        end;
        Close;
      end;
    except
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Judge_MutilSelect(const Kcdm, Tx: WideString);
var
  tmp_query:tadoquery;
  fs:Double;
  //Tx:String;
begin
  try
    //Tx := Get_Tx(Kcdm,'多项选择题');
    fs := Get_Tx_Fs(Kcdm,Tx);

    tmp_query := tadoquery.Create(nil);
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT tk.题型, tk.标准答案, sj.答案, sj.得分 FROM 考生试卷表 sj INNER JOIN ');
        sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx)+' AND tk.课程号='+quotedstr(kcdm));
        Open;
        while not eof do
        begin
          Edit;
          if UpperCase(fieldbyname('标准答案').AsString) = UpperCase(fieldbyname('答案').Asstring) then
             fieldbyname('得分').Value := fs
          else
             fieldbyname('得分').Value := 0;
          Post;
          Next;
        end;
        Close;
      end;
    except
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Judge_Pbt(const Kcdm, Tx, Sub_Count: WideString);
var
  tmp_query,tmp_query2:tadoquery;
  fs:Double;
  icount :Integer;
  //Tx:String;
begin
  try
    //Tx := Get_Tx(Kcdm,'匹配题');
    fs := Get_Tx_Fs(Kcdm,Tx);

    tmp_query := tadoquery.Create(nil);
    tmp_query2 := tadoquery.Create(nil);
    try

      with tmp_query2 do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT tk.ID FROM 考生试卷表 sj INNER JOIN ');
        sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx));
        sql.add(' and tk.课程号='+quotedstr(kcdm)+' and sj.小题号=0 order by sj.题号');
        Open;
      end;

      while not tmp_query2.Eof do
      begin
        iCount := Get_Sub_St_Count(tmp_query2.fieldbyname('ID').AsString);
        
        if iCount = 0 then
          iCount := 1;

        with tmp_query do
        begin
          close;
          connection := adoconnection1;

          sql.clear;
          sql.add('SELECT tk.题型, tk.标准答案, sj.答案, sj.得分 FROM 考生试卷表 sj INNER JOIN ');
          sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx)+' AND tk.课程号='+quotedstr(kcdm));
          sql.Add('and sj.小题号>0 order by sj.题号,sj.小题号');
          Open;
          while not eof do
          begin
            Edit;
            if UpperCase(fieldbyname('标准答案').AsString) = UpperCase(fieldbyname('答案').Asstring) then
               fieldbyname('得分').Value := fs/iCount
            else
               fieldbyname('得分').Value := 0;
            Post;
            Next;
          end;
          Close;
        end;

        tmp_query2.Next;

      end; //while not tmp_query2.eof
      tmp_query2.Close;

    except
    end;

  finally
    tmp_query.Free;
    tmp_query2.Free;
  end;
end;

procedure TKSTK_Srv.Judge_Tkt(const Kcdm, Tx, Qxdxx: WideString);
var                                                              // else 不区分大小写
  tmp_query:tadoquery;
  fs:Double;
  //Tx:String;
begin
  try
    //Tx := Get_Tx(Kcdm,'填空题');
    fs := Get_Tx_Fs(Kcdm,Tx);

    tmp_query := tadoquery.Create(nil);
    try
      with tmp_query do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT tk.题型, tk.标准答案, sj.答案, sj.得分 FROM 考生试卷表 sj INNER JOIN ');
        sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx)+' AND tk.课程号='+quotedstr(kcdm));
        Open;
        while not eof do
        begin
          Edit;
          if Qxdxx = '1' then //如果区分大小写
          begin
            if Trim(fieldbyname('标准答案').AsString) = Trim(fieldbyname('答案').Asstring) then
               fieldbyname('得分').Value := fs
            else
               fieldbyname('得分').Value := 0;
          end else
          begin
            if UpperCase(Trim(fieldbyname('标准答案').AsString)) = UpperCase(Trim(fieldbyname('答案').Asstring)) then
               fieldbyname('得分').Value := fs
            else
               fieldbyname('得分').Value := 0;
          end;

          Post;
          Next;
        end;
        Close;
      end;
    except
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.Judge_Dwt(const Kcdm, Tx, Sub_Count: WideString);
var
  tmp_query,tmp_query2:tadoquery;
  fs:Double;
  icount :Integer;
  //Tx:String;
begin
  try
    //Tx := Get_Tx(Kcdm,'短文题');
    fs := Get_Tx_Fs(Kcdm,Tx);

    tmp_query := tadoquery.Create(nil);
    tmp_query2 := tadoquery.Create(nil);
    try

      with tmp_query2 do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT tk.ID FROM 考生试卷表 sj INNER JOIN ');
        sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx));
        sql.add(' and tk.课程号='+quotedstr(kcdm)+' and sj.小题号=0 order by sj.题号');
        Open;
      end;

      while not tmp_query2.Eof do
      begin
        iCount := Get_Sub_St_Count(tmp_query2.fieldbyname('ID').AsString);
        
        if iCount = 0 then
          iCount := 1;

        with tmp_query do
        begin
          close;
          connection := adoconnection1;

          sql.clear;
          sql.add('SELECT tk.题型, tk.标准答案, sj.答案, sj.得分 FROM 考生试卷表 sj INNER JOIN ');
          sql.add('题库表 tk ON sj.题目ID = tk.ID WHERE tk.题型='+quotedstr(Tx)+' AND tk.课程号='+quotedstr(kcdm));
          sql.Add('and sj.小题号>0 order by sj.题号,sj.小题号');
          Open;
          while not eof do
          begin
            Edit;
            if UpperCase(fieldbyname('标准答案').AsString) = UpperCase(fieldbyname('答案').Asstring) then
               fieldbyname('得分').Value := fs/iCount
            else
               fieldbyname('得分').Value := 0;
            Post;
            Next;
          end;
          Close;
        end;

        tmp_query2.Next;

      end; //while not tmp_query2.eof
      tmp_query2.Close;

    except
    end;

  finally
    tmp_query.Free;
    tmp_query2.Free;
  end;
end;

procedure TKSTK_Srv.Sum_Fs(const Kcdm: WideString);
var
  tmp_query,tmp_query2:tadoquery;
  //fs:Double;
begin
  try
    tmp_query := tadoquery.Create(nil);
    tmp_query2 := tadoquery.Create(nil);
    try

      with tmp_query2 do
      begin
        close;
        connection := adoconnection1;

        sql.clear;
        sql.add('SELECT sj.课程号,sj.学号,Sum(sj.得分) as 成绩 FROM 考生试卷表 sj ');
        sql.add('where sj.课程号='+quotedstr(kcdm)+' group by sj.课程号,sj.学号');
        Open;
      end;

      while not tmp_query2.Eof do
      begin
        //fs := tmp_query2.fieldbyname('成绩').Value;

        with tmp_query do
        begin
          close;
          connection := adoconnection1;

          sql.clear;
          sql.add('Select 学号,考试状态,成绩 from 考生考试表 ');
          sql.add('where 课程号='+quotedstr(kcdm)+' and 学号='+quotedstr(tmp_query2.fieldbyname('学号').AsString));
          Open;
          if fieldCount>0 then
          begin
            Edit;
            if fieldbyname('考试状态').AsString<>'舞弊' then
              fieldbyname('成绩').Value := tmp_query2.fieldbyname('成绩').Value
            else
               fieldbyname('成绩').Value := 0;
            Post;
          end;
          Close;
        end;

        tmp_query2.Next;

      end; //while not tmp_query2.eof
      tmp_query2.Close;

    except
    end;

  finally
    tmp_query.Free;
    tmp_query2.Free;
  end;
end;

function TKSTK_Srv.AddTimeLength(const Kcdm: WideString): OleVariant;
begin
  Result := 15;
end;

function TKSTK_Srv.AddTimePwd(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 补时密码 from 考试配置表');
      sql.add('where 课程号='+quotedstr(Kcdm));
      open;
      Result := fields[0].Value;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;


function TKSTK_Srv.Get_Kgt_Cj(const Kcdm, KaoHao: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    Result := 0.0;

    with tmp_query do
    begin
      close;
      connection := adoconnection1;

      sql.clear;
      sql.add('SELECT 成绩 FROM VIEW_主观题成绩');
      sql.add('where 课程号='+quotedstr(kcdm)+' and 学号='+quotedstr(KaoHao));
      Open;
      Result := fieldbyname('成绩').AsFloat;
      Close;
    end;

  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Get_Is_All_Jj(const Kcdm: WideString): OleVariant;
begin
  Result := Pos(Kcdm+',',All_Jj_Kcdm)>0;
end;

procedure TKSTK_Srv.Set_All_Jj_Kcdm(const Kcdm, Is_All_Jj: WideString);
var
  i:integer;
begin
  i := Pos(Kcdm+',',All_Jj_Kcdm);
  
  if Is_All_Jj = '是' then
  begin
    if i<1 then
       All_Jj_Kcdm := All_Jj_Kcdm+Kcdm+',';
  end else
  begin
    if i>0 then
       Delete(All_Jj_Kcdm,i,length(Kcdm+','));
  end;
end;

function TKSTK_Srv.Get_CON_Db_IP: OleVariant;
begin
  Result := Get_Host_Name;
end;

function TKSTK_Srv.Get_CON_Db_Name: OleVariant;
begin
  Result := Get_Db_Name;
end;

function TKSTK_Srv.Get_CON_Sa_Name: OleVariant;
begin
  Result := Get_Sa_Name;
end;

function TKSTK_Srv.Get_CON_Sa_Pwd: OleVariant;
begin
  Result := Get_Sa_Pwd;
end;

function TKSTK_Srv.Get_Cur_Kddm: OleVariant;
var
  tmp_query:tadoquery;
begin
  try
    tmp_query := tadoquery.Create(nil);
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.Add('select 考点代码 from 考点信息表');
      Open;
      if RecordCount = 0 then
        Result := '无考点'
      else if RecordCount > 1  then
        Result := '考试中心'
      else
         Result := fields[0].Value;
      Close;
    end;
  finally
    tmp_query.Free;
  end;
end;

procedure TKSTK_Srv.tmp_DataSetProviderGetData(Sender: TObject;
  DataSet: TCustomClientDataSet);
begin
  UnEncypt_Data(DataSet);
end;

procedure TKSTK_Srv.tmp_DataSetProviderUpdateData(Sender: TObject;
  DataSet: TCustomClientDataSet);
begin
  Encypt_Data(DataSet);
end;

procedure TKSTK_Srv.tmp_DataSetProviderUpdateError(Sender: TObject;
  DataSet: TCustomClientDataSet; E: EUpdateError; UpdateKind: TUpdateKind;
  var Response: TResolverResponse);
begin
  raise  E;
end;

procedure TKSTK_Srv.Bxdab_ADODataSetBeforePost(DataSet: TDataSet);
begin
  //ShowMessage(Tkb_AdoDataSet.FieldByName('id').AsString);
  //DataSet.FieldByName('pid').Value := Tkb_AdoDataSet.FieldByName('id').Value;
end;

procedure TKSTK_Srv.Tkfjb_ADODataSetBeforePost(DataSet: TDataSet);
begin
  //ShowMessage(Tkb_AdoDataSet.FieldByName('id').AsString);
  //DataSet.FieldByName('pid').Value := Tkb_AdoDataSet.FieldByName('id').Value;
end;

procedure TKSTK_Srv.Gjcxb_ADODataSetBeforePost(DataSet: TDataSet);
begin
  //ShowMessage(Tkfjb_AdoDataSet.FieldByName('id').AsString);
  //DataSet.FieldByName('pid').Value := Tkfjb_AdoDataSet.FieldByName('id').Value;
end;

procedure TKSTK_Srv.Tkfjb_ADODataSetAfterOpen(DataSet: TDataSet);
begin
  if not Gjcxb_AdoDataSet.Active then
    Gjcxb_AdoDataSet.Open;
end;

function TKSTK_Srv.Get_Tk_MaxID: Integer;
begin
  Result := Adoconnection1.Execute('select max(id)+1 from 题库表').Fields[0].value;
end;

function TKSTK_Srv.Get_Tkb_MaxID: Integer;
begin
  Adoconnection1.Execute('update InkeyTable set Tkb_ID=Tkb_ID+1');
  Result := Adoconnection1.Execute('select Tkb_ID from InkeyTable').Fields[0].value;
end;


function TKSTK_Srv.Get_Tkfjb_MaxID: Integer;
begin
  Adoconnection1.Execute('update InkeyTable set Tkfjb_ID=Tkfjb_ID+1');
  Result := Adoconnection1.Execute('select Tkfjb_ID from InkeyTable').Fields[0].value;

end;

procedure TKSTK_Srv.Refresh_InKeyTable;
begin
  Refresh_MaxID(ADOConnection1);
end;

function TKSTK_Srv.Set_Ks_Code(const Kcdm: WideString): OleVariant;
var
  startTime,endTime,
  KsTimeLength,
  checkCode,checkCode2:string;
begin
  Result := False;
  checkCode :='';
  with tadoquery.Create(nil) do
  try
    connection := adoconnection1;
    SQL.Text := 'select 考试开始日期,考试结束日期,考试时长,验证码 from 考试配置表 where 课程号='+quotedstr(Kcdm);
    Open;
    if Recordcount=0 then
      Exit
    else begin
      startTime := FormatDateTime('yyyy-mm-dd hh:nn',Fields[0].AsDateTime);
      endTime := FormatDateTime('yyyy-mm-dd hh:nn',Fields[1].AsDateTime);
      //KsTimeLength := IntToStr(Fields[2].AsInteger);
      checkCode := Fields[3].AsString;
    end;
  finally
    Free;
  end;

  KsTimeLength := IntToStr(Get_Ks_Time_Length(Kcdm));
  checkCode2 := startTime+','+endTime+','+KsTimeLength;
  checkCode := EnCrypt(checkCode2);//加码

  with tadoquery.Create(nil) do
  try
    connection := adoconnection1;
    SQL.Text := 'update 考试配置表 set 验证码=CONVERT(varbinary(250),'+quotedstr(checkCode)+') where 课程号='+quotedstr(Kcdm);
    try
      ExecSQL;
      Result := True;
    except
    end;
  finally
    Free;
  end;
end;

function TKSTK_Srv.Get_Ks_Time_Length(const Kcdm: WideString): OleVariant;
var
  tmp_query:tadoquery;
begin
  tmp_query := tadoquery.Create(nil);
  try
    Result := False;
    with tmp_query do
    begin
      close;
      connection := adoconnection1;
      sql.clear;
      sql.add('select 考试时长 from 考试配置表 where 课程号='+quotedstr(Kcdm));
      Open;
      Result := StrToIntDef(Fields[0].AsString,0);
    end;
  finally
    tmp_query.Free;
  end;
end;

function TKSTK_Srv.Set_Dzcs_Count(const Kcdm: WideString;
  iCount: Integer): OleVariant;
var
  sqlStr:string;
begin
  Result := 400;
  with tadoquery.Create(nil) do
  try
    connection := adoconnection1;
    sqlStr := 'select count(*) from 打字配置表 where 课程号='+quotedstr(Kcdm);
    SQL.Text := sqlStr;
    Open;
    if Fields[0].AsInteger>0 then
      sqlStr := 'update 打字配置表 set 字数='+IntToStr(iCount)+' where 课程号='+quotedstr(Kcdm)
    else
      sqlStr := 'Insert into 打字配置表 (课程号,字数) values('+quotedstr(Kcdm)+','+IntToStr(iCount)+')';
    Close;
    SQL.Text := sqlStr;
    try
      ExecSQL;
      Result := True;
    Except
      Result := False;
    end;
  finally
    Free;
  end;
end;

function TKSTK_Srv.Get_Dzcs_Count(const Kcdm: WideString): Integer;
begin
  Result := 400;
  with tadoquery.Create(nil) do
  try
    connection := adoconnection1;
    SQL.Text := 'select 字数 from 打字配置表 where 课程号='+quotedstr(Kcdm);
    try
      Open;
    Except
      Exit;
    end;
    if Recordcount>0 then
    begin
      Result := Fields[0].AsInteger;
    end else
      Result := 400;
  finally
    Free;
  end;
end;

initialization
  TComponentFactory.Create(ComServer, TKSTk_Srv,
    Class_KSTk_Srv, ciMultiInstance, tmApartment);
end.

